package tests;

import Common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.io.IOException;

public class adminPage extends Utils {
    public static String url = "http://127.0.0.1:8080/admin.html";

    public adminPage() {
        super(url);
    }

    //检查人员列表是否加载成功
    public void userPageRight() throws InterruptedException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#userList")).click();
        Thread.sleep(2000);
        //切换iframe,参数为iframe标签的id或者name，此处写的是id
        driver.switchTo().frame("contentFrame");
        //通过查看页面元素是否存在来检查页面加载成功与否
        driver.findElement(By.cssSelector("body > div > h2"));
    }

    //检查奖品列表是否加载成功
    public void prizePageRight() throws InterruptedException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("body > div.cont-box > div.sidebar > ul > li:nth-child(2) > ul > li:nth-child(1) > a")).click();
        Thread.sleep(2000);
        //切换iframe,参数为iframe标签的id或者name，此处写的是id
        driver.switchTo().frame("contentFrame");
        //通过查看页面元素是否存在来检查页面加载成功与否
        driver.findElement(By.cssSelector("body > div > h2"));
    }


    //创建抽奖活动
    public void createActivity() throws InterruptedException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#createActivity")).click();
        Thread.sleep(3000);
        //切换iframe,参数为iframe标签的id或者name，此处写的是id
        driver.switchTo().frame("contentFrame");
        driver.findElement(By.cssSelector("#activityName")).sendKeys("自动化测试活动");
        driver.findElement(By.cssSelector("#description")).sendKeys("自动化测试活动描述");
        //圈选奖品
        driver.findElement(By.cssSelector("#buttonPrizes")).click();
        driver.findElement(By.cssSelector("#prize-13")).click();
        driver.findElement(By.cssSelector("#prizesModal > div > div.form-btn-box > button.btn.btn-primary")).click();
        //圈选人员
        driver.findElement(By.cssSelector("#buttonUsers")).click();
        driver.findElement(By.cssSelector("#user-4")).click();
        driver.findElement(By.cssSelector("#usersModal > div > div.form-btn-box > button.btn.btn-primary")).click();
        //创建活动
        driver.findElement(By.cssSelector("#createActivity")).click();
        //处理警告弹窗
        Thread.sleep(2000);
        Alert alert = driver.switchTo().alert();
        alert.accept();

    }
    //抽奖
    public void drawPrize() throws InterruptedException {
        driver.navigate().refresh();
        Thread.sleep(3000);
        //切换iframe,参数为iframe标签的id或者name，此处写的是id
        driver.switchTo().frame("contentFrame");
        driver.findElement(By.cssSelector("#activities > div:nth-child(1) > div.active > p > a")).click();
        //切换窗⼝
        String curHandle = driver.getWindowHandle();
        for (String w : driver.getWindowHandles()) {
            if (w != curHandle) {
                driver.switchTo().window(w);
            }
        }
        Thread.sleep(2000);
         //点击开始抽奖
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn")).click();
        //点击“以抽完，下一步”
        driver.findElement(By.cssSelector("#container > div.opt-box > span.btn.next-btn")).click();
        Thread.sleep(2000);
        //关闭窗⼝
        driver.close();
        driver.switchTo().window(curHandle);
    }
    //展示中奖者
    public void showWinningRecords() throws InterruptedException, IOException {

        driver.navigate().refresh();
        Thread.sleep(2000);
        //切换iframe,参数为iframe标签的id或者name，此处写的是id
        driver.switchTo().frame("contentFrame");
        driver.findElement(By.cssSelector("#activities > div:nth-child(1) > div.inactive > p > a")).click();
        //切换窗⼝
        String curHandle = driver.getWindowHandle();
        for (String w : driver.getWindowHandles()) {
            if (w != curHandle) {
                driver.switchTo().window(w);
            }
        }
        Thread.sleep(2000);
        //关闭窗⼝
        driver.close();
    }
}
